home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / bildschirmschoner / beyondthedark / developer / source / rain / rain.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  5KB  |  227 lines

  1. /* Rain Library */
  2.  
  3. #include <exec/memory.h>
  4. #include <exec/execbase.h>
  5. #include <graphics/gfxbase.h>
  6. #include <intuition/intuitionbase.h>
  7. #include <libraries/iffparse.h>
  8. #include <utility/tagitem.h>
  9.  
  10. #include <clib/macros.h>
  11.  
  12. #define __USE_SYSBASE 42
  13.  
  14. #include <proto/exec.h>
  15. #include <proto/graphics.h>
  16. #include <proto/intuition.h>
  17. #include <proto/utility.h>
  18.  
  19. #include <BTD.h>
  20.  
  21. struct IntuitionBase *IntuitionBase;
  22. struct GfxBase *GfxBase;
  23.  
  24. /* #define DEBUG YES */
  25.  
  26. #ifdef DEBUG 
  27.  
  28. void KPrintF(char *,...);
  29.  
  30. #define DEBUG_PRINTF(a,b)  KPrintF(a,b);
  31. #define DEBUG_PRINT(a)     KPrintF(a)
  32. #else
  33. #define DEBUG_PRINTF(a,b)
  34. #define DEBUG_PRINT(a)
  35. #endif
  36.  
  37. #define DTAG(o) (BTD_Client+(o))
  38.  
  39. #define RP_Drops DTAG(0)
  40. #define RP_Colors DTAG(1)
  41.  
  42. #define DEF_COLORS 8L
  43. #define MAX_COLORS 128L
  44.  
  45. #define DEF_DROPS 8L
  46. #define MAX_DROPS 100L
  47.  
  48. #ifndef max
  49. #define max( x, y ) ((x)>(y)?(x):(y))
  50. #endif
  51.  
  52. #define FindTagData(l,t,d) GetTagData((t),(d),(l))
  53.  
  54. struct BTDInteger RainIntParams[] =
  55.  {
  56.   RP_Drops,"Drops",BTDPT_INTEGER,DEF_DROPS,1L,MAX_DROPS,TRUE,
  57.   RP_Colors,"Colors",BTDPT_INTEGER,DEF_COLORS,1L,MAX_COLORS,TRUE
  58.  };
  59.  
  60. struct BTDNode *RainParams[] = 
  61.  {
  62.   &RainIntParams[0].BI_Node,
  63.   &RainIntParams[1].BI_Node,
  64.   NULL
  65.  };
  66.  
  67. struct BTDInfo RainInfo =
  68.  {
  69.   BTDI_Revision,MAKE_ID('R','A','I','N'),
  70.   "Rain Blanker","I love it's rain' again..","Markus Illenseer 1994",
  71.   RainParams
  72.  };
  73.  
  74. struct RainStruct
  75.  {
  76.   struct BTDDrawInfo *BTDDrawInfo;
  77.   LONG Colors;
  78.   LONG Drops;
  79.   LONG DropCount;
  80.   LONG RandN,RandF,RandI;
  81.  };
  82.  
  83. /* library stuff */
  84.  
  85. char MyBlankerName[] = "rain.btd";
  86. char MyBlankerID[]   = "Rainy Blanker V" VERSION "." REVISION " for BTD";
  87.  
  88. LONG MyBlankerLibInit(void)
  89.  
  90. {
  91.  if (GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37L))
  92.   {
  93.    if (IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L))
  94.     {
  95.      if (UtilityBase=OpenLibrary("utility.library",37L)) return TRUE;
  96.      CloseLibrary (&IntuitionBase->LibNode);
  97.     }
  98.    CloseLibrary (&GfxBase->LibNode);
  99.   }
  100.  return FALSE;
  101. }
  102.  
  103. void MyBlankerLibFree(void)
  104.  
  105. {
  106.  CloseLibrary (UtilityBase);
  107.  CloseLibrary (&IntuitionBase->LibNode);
  108.  CloseLibrary (&GfxBase->LibNode);
  109. }
  110.  
  111. struct BTDInfo *QueryMyBlanker(void)
  112.  
  113. {
  114.  return &RainInfo;
  115. }
  116.  
  117. void __regargs InitRandom(struct RainStruct *RP,ULONG Instance)
  118.  
  119. {
  120.  ULONG Time[2];
  121.  
  122.  CurrentTime (&Time[0],&Time[1]);
  123.  RP->RandN=(LONG)Time[0];
  124.  
  125.  if (Time[1]<1024L) Time[1]|=1;
  126.  else Time[1]>>=10;
  127.  Time[1]^=Instance;
  128.  
  129.  RP->RandF=4*Time[1]+1;
  130.  RP->RandI=2*Time[1]+1;
  131. }
  132.  
  133. WORD __regargs Random(struct RainStruct *RP,WORD Max)
  134.  
  135. {
  136.  RP->RandN=RP->RandF*RP->RandN+RP->RandI;
  137.  if (RP->RandN<0L) RP->RandN=-RP->RandN;
  138.  
  139.  return (WORD)(RP->RandN%Max);
  140. }
  141.  
  142. struct RainStruct *InitMyBlanker(struct TagItem *TagList)
  143.  
  144. {
  145.  struct RainStruct *RP;
  146.  struct BTDDrawInfo *BTDDrawInfo;
  147.  ULONG *Error,Dummy,Instance,Index,Step;
  148.  
  149.  if ((BTDDrawInfo=(struct BTDDrawInfo *)
  150.                    FindTagData(TagList,BTD_DrawInfo,NULL))==NULL) return NULL;
  151.  Error=(LONG *)FindTagData(TagList,BTD_Error,(ULONG)&Dummy);
  152.  if ((RP=AllocVec(sizeof(struct RainStruct),MEMF_PUBLIC|MEMF_CLEAR))==NULL)
  153.   {
  154.    *Error=BTDERR_Memory;
  155.    return NULL;
  156.   }
  157.  
  158.  RP->BTDDrawInfo=BTDDrawInfo;
  159.  Instance=FindTagData(TagList,BTD_Instance,0L);
  160.  
  161.  InitRandom(RP,Instance);
  162.  
  163.  RP->Colors=FindTagData(TagList,RP_Colors,DEF_COLORS);
  164.  RP->Drops=FindTagData(TagList,RP_Drops,DEF_DROPS);
  165.  RP->DropCount=0L;
  166.  
  167.  Step=(MAX_COLORS/(RP->Colors));
  168.  
  169.  for(Index=0; Index<RP->Colors; Index++)
  170.   {
  171.    BTDDrawInfo->BDI_Red[BTDDrawInfo->BDI_Pens[Index]]=64+Step*Index;
  172.    BTDDrawInfo->BDI_Green[BTDDrawInfo->BDI_Pens[Index]]=64+Step*Index;
  173.    BTDDrawInfo->BDI_Blue[BTDDrawInfo->BDI_Pens[Index]]=64+Step*Index;
  174.    BTDDrawInfo->BDI_Changed[BTDDrawInfo->BDI_Pens[Index]]=TRUE;
  175.   }
  176.  
  177.  return RP;
  178. }
  179.  
  180. void EndMyBlanker(struct RainStruct *RP)
  181.  
  182. {
  183. DEBUG_PRINT("Rain: FreeMem\n");
  184.  FreeVec (RP);
  185. }
  186.  
  187. void AnimMyBlanker(struct RainStruct *RP)
  188.  
  189. {
  190.  
  191.  LONG Rad,x,y,Step,Index;
  192.  
  193.   if(RP->DropCount++ >RP->Drops)
  194.    {
  195.     SetAPen (RP->BTDDrawInfo->BDI_RPort,BTD_BgPen);
  196.     RectFill (RP->BTDDrawInfo->BDI_RPort,
  197.               RP->BTDDrawInfo->BDI_Left,
  198.               RP->BTDDrawInfo->BDI_Top,
  199.               RP->BTDDrawInfo->BDI_Left+RP->BTDDrawInfo->BDI_Width-1,
  200.               RP->BTDDrawInfo->BDI_Top+RP->BTDDrawInfo->BDI_Height-1);
  201.     RP->DropCount=0L;
  202.    }
  203.  
  204.    Rad = Random(RP, (RP->BTDDrawInfo->BDI_Width-1)/18)+(RP->BTDDrawInfo->BDI_Width-1)/35;
  205.    x = Random(RP,(RP->BTDDrawInfo->BDI_Width-1)-2*Rad)+Rad+RP->BTDDrawInfo->BDI_Left;
  206.    y = Random(RP,(RP->BTDDrawInfo->BDI_Height-1)-2*Rad)+Rad +RP->BTDDrawInfo->BDI_Top;
  207.     
  208.    Step=max( RP->BTDDrawInfo->BDI_Width/160, 1 );
  209.    WaitTOF();
  210.    for(Index=0; Index<Rad; Index += Step )
  211.     {
  212.       SetAPen(RP->BTDDrawInfo->BDI_RPort,RP->BTDDrawInfo->BDI_Pens[Random(RP,RP->Colors-1)]);
  213.       DrawEllipse(RP->BTDDrawInfo->BDI_RPort,x,y,Index,Index);
  214.       if(Index>0)
  215.        {
  216.         SetAPen (RP->BTDDrawInfo->BDI_RPort,BTD_BgPen);
  217.         DrawEllipse(RP->BTDDrawInfo->BDI_RPort,x,y,Index-Step,Index-Step);
  218.        }
  219.     }
  220. }
  221.  
  222. ULONG PenCountMyBlanker(struct TagItem *TagList)
  223.  
  224. {
  225.  return FindTagData(TagList,RP_Colors,DEF_COLORS);;
  226. }
  227.